package com.harvest.scientific.ladder.dialog

import android.content.Context
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.databinding.CommonDialogBinding
import com.harvest.scientific.ladder.util.LogUtils
import java.lang.reflect.ParameterizedType

/**
 * @author：zenglingwen
 * @date：2024-06-04 22:25
 * @desc：
 */
open class CommonDialog<VB: ViewBinding>(context:Context): BaseDialog(context){
    protected lateinit var _binding: VB
    protected lateinit var _parentBinding: CommonDialogBinding
    override fun initLayout(): Int {
        return R.layout.common_dialog
    }

    override fun initView() {
        _parentBinding = CommonDialogBinding.bind(rootView)
        _parentBinding.root.setOnClickListener {
            dismissDialog()
        }
        _parentBinding.topCloseContainer.setOnClickListener {
            dismissDialog()
        }
        createViewBinding()
        _parentBinding.rootView.addView(_binding.root)
        LogUtils.d("binding = $_parentBinding")

    }
    open protected fun createViewBinding() {

        val clazz: Class<*> =  (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>

        try {
            _binding = clazz.getMethod(
                "inflate", LayoutInflater::class.java
            ).invoke(null, layoutInflater) as VB

        } catch (e: Exception) {
            e.printStackTrace()
            throw IllegalArgumentException("无法通过反射创建ViewBinding对象")
        }

    }

    override fun initEvent() {
    }
}